﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using My_TV_Manager.Model;

namespace My_TV_Manager.View.ContentPanel.Track
{
    [System.ComponentModel.DesignerCategory("")]

    public class TrackSeriesPanel : Panel
    {
        #region Private Data

        private Controller controller;

        private SeriesCategoryPanel categoryPanel;
        private Panel seriesDisplayPanel;
        private List<TrackedSeriesPanel> trackedSeriesPanels;
        private SearchResultSortCategory sortCategory;

        #endregion

        #region Properties

        public SearchResultSortCategory SortCategory
        {
            get { return sortCategory; }
            set 
            {
                sortCategory = value;
                foreach (TrackedSeriesPanel series in trackedSeriesPanels)
                {
                    series.SortCategory = value;
                }
                trackedSeriesPanels.Sort();
                trackedSeriesPanels.Reverse();
                for (int i = 0; i < trackedSeriesPanels.Count; i++)
                {
                    trackedSeriesPanels[i].Top = i * (controller.SearchResultDisplayHeight + 10);
                }
            }
        }

        #endregion

        #region Constructors

        // Constructor using controller for program data
        public TrackSeriesPanel(Size size, Color color, Controller controller)
        {
            this.Size = size;
            this.BackColor = color;
            this.controller = controller;
            controller.TrackSeriesPanel = this;

            categoryPanel = new SeriesCategoryPanel(new Size(this.ClientSize.Width - 40, 30));
            categoryPanel.Location = new Point(10, 10);

            trackedSeriesPanels = new List<TrackedSeriesPanel>();
            this.SortCategory = SearchResultSortCategory.Alphabetic;

            seriesDisplayPanel = new Panel();
            seriesDisplayPanel.Location = new Point(controller.SearchResultDisplayPadding, 
                                                    categoryPanel.Location.Y +
                                                    categoryPanel.ClientSize.Height +
                                                    controller.SearchResultDisplayOffset);
            seriesDisplayPanel.Size = new Size(this.ClientSize.Width - 
                                               seriesDisplayPanel.Location.X - 
                                               controller.SearchResultDisplayPadding,
                                               this.ClientSize.Height - 
                                               seriesDisplayPanel.Location.Y - 
                                               controller.SearchResultDisplayPadding);
            seriesDisplayPanel.BackColor = Color.Transparent;
            seriesDisplayPanel.AutoScroll = true;
            seriesDisplayPanel.MouseMove += new MouseEventHandler(seriesDisplayPanel_MouseMove);

            this.Controls.Add(categoryPanel);
            this.Controls.Add(seriesDisplayPanel);

            BuildSeriesDisplayPanel();
        }

        #endregion

        #region Event Handlers

        private void seriesDisplayPanel_MouseMove(object sender, MouseEventArgs e)
        {
            foreach (TrackedSeriesPanel displayPanel in trackedSeriesPanels)
            {
                if (displayPanel.ClientRectangle.Contains(PointToClient(MousePosition)))
                {
                    displayPanel.BackColor = controller.SearchResultActiveColor;
                }
                else { displayPanel.BackColor = controller.SearchResultPassiveColor; }
            }
        }

        #endregion

        #region Functions

        public void AddTrackedSeries(SearchResult series)
        {
            TrackedSeriesPanel panel = new TrackedSeriesPanel(series,
                                                              controller.SearchResultBannerDisplayRatio,
                                                              new Size(seriesDisplayPanel.Width - 20,
                                                                       controller.SearchResultDisplayHeight),
                                                              controller);
            panel.Top = trackedSeriesPanels.Count * 
                        (controller.SearchResultDisplayHeight + 
                         controller.SearchResultDisplayOffset);
            panel.BackColor = controller.SearchResultPassiveColor;

            trackedSeriesPanels.Add(panel);
            seriesDisplayPanel.Controls.Add(trackedSeriesPanels[trackedSeriesPanels.Count - 1]);

            trackedSeriesPanels.Sort();
            for (int i = 0; i < trackedSeriesPanels.Count; i++)
            {
                trackedSeriesPanels[i].Top = i * 
                                             (controller.SearchResultDisplayHeight + 
                                              controller.SearchResultDisplayOffset);
            }
        }

        public void RemoveTrackedSeries(SearchResult series)
        {
            foreach (TrackedSeriesPanel tracked in trackedSeriesPanels)
            {
                if (tracked.SearchResult.SeriesName == series.SeriesName)
                {
                    seriesDisplayPanel.Controls.Remove(tracked);
                    trackedSeriesPanels.Remove(tracked);

                    for (int i = 0; i < trackedSeriesPanels.Count; i++)
                    {
                        trackedSeriesPanels[i].Top = i * 
                                                     (controller.SearchResultDisplayHeight + 
                                                      controller.SearchResultDisplayOffset);
                    }

                    break;
                }
            }
        }

        private void BuildSeriesDisplayPanel()
        {
            seriesDisplayPanel.Controls.Clear();

            var trackedSeries = controller.TrackedSeries;

            foreach (SearchResult series in trackedSeries)
            {
                TrackedSeriesPanel panel = new TrackedSeriesPanel(series,
                                                                  controller.SearchResultBannerDisplayRatio,
                                                                  new Size(seriesDisplayPanel.Width - 20,
                                                                           controller.SearchResultDisplayHeight),
                                                                  controller);
                panel.Top = trackedSeriesPanels.Count * 
                            (controller.SearchResultDisplayHeight + 
                             controller.SearchResultDisplayOffset);

                trackedSeriesPanels.Add(panel);
                seriesDisplayPanel.Controls.Add(trackedSeriesPanels[trackedSeriesPanels.Count - 1]);
            }
        }

        #endregion
    }
}
